package com.cskaoyan.user.service.impl;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.commons.exception.ProcessException;
import com.cskaoyan.user.dal.entitys.Member;
import com.cskaoyan.user.dal.entitys.UserVerify;
import com.cskaoyan.user.dal.persistence.MemberMapper;
import com.cskaoyan.user.dal.persistence.UserVerifyMapper;
import com.cskaoyan.user.form.UserRegisterRequest;
import com.cskaoyan.user.form.UserVerifyRequest;
import com.cskaoyan.user.properties.MailProperties;
import com.cskaoyan.user.service.IUserRegisterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Auther: Yang Yi-zhou
 * @Date: 2022/9/21 10:11
 * @Description:
 */
@Service
@Slf4j
public class UserRegisterServiceImpl implements IUserRegisterService {
    @Autowired
    MemberMapper memberMapper;
    @Autowired
    UserVerifyMapper userVerifyMapper;
    @Autowired
    MailProperties mailProperties;
    @Autowired
    JavaMailSender javaMailSender;

    @Override
    public void register(UserRegisterRequest request) {
        String userName = request.getUserName();
        String userPwd = request.getUserPwd();
        String email = request.getEmail();
        // 校验一下邮箱是否已存在
        Example example = new Example(Member.class);
        example.createCriteria().andEqualTo("email", email);
        List<Member> members = memberMapper.selectByExample(example);
        if (members.size() > 0) {
            throw new BizException(SysRetCodeConstants.EMAIL_ALREADY_EXIST.getCode(), SysRetCodeConstants.EMAIL_ALREADY_EXIST.getMessage());
        }
        // 校验用户名是否已存在
        Example example1 = new Example(Member.class);
        example1.createCriteria().andEqualTo("username", userName);
        List<Member> members1 = memberMapper.selectByExample(example1);
        if (members1.size() > 0) {
            throw new BizException(SysRetCodeConstants.USERNAME_ALREADY_EXIST.getCode(), SysRetCodeConstants.USERNAME_ALREADY_EXIST.getMessage());
        }
        // 插入到用户表
        Member member = new Member();
        member.setUsername(userName);
        member.setEmail(email);
        String md5DigestAsHex = DigestUtils.md5DigestAsHex(userPwd.getBytes());
        member.setPassword(md5DigestAsHex);
        member.setUpdated(new Date());
        member.setCreated(new Date());
        member.setIsVerified("N");
        member.setState(1);
        int affectedRows = memberMapper.insertSelective(member);
        if (affectedRows != 1) {
            throw new ProcessException(SysRetCodeConstants.USER_REGISTER_FAIL.getCode(), SysRetCodeConstants.USER_REGISTER_FAIL.getMessage());
        }
        // 插入到用户验证表
        UserVerify userVerify = new UserVerify();
        userVerify.setUsername(userName);
        userVerify.setRegisterDate(new Date());
        userVerify.setIsVerify("N");
        userVerify.setIsExpire("N");
        String uuid = UUID.randomUUID().toString();
        userVerify.setUuid(uuid);
        int affectedRows1 = userVerifyMapper.insertSelective(userVerify);
        if (affectedRows1 != 1) {
            throw new ProcessException(SysRetCodeConstants.USER_REGISTER_FAIL.getCode(), SysRetCodeConstants.USER_REGISTER_FAIL.getMessage());
        }
        // 异步发送激活邮件
        new Thread(new Runnable() {
            @Override
            public void run() {
                // 发激活邮件
                SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
                simpleMailMessage.setSubject("都来买商城用户注册激活");
                simpleMailMessage.setTo(email);
                simpleMailMessage.setFrom(mailProperties.getUsername());
                simpleMailMessage.setText(String.format(mailProperties.getText(), userName, uuid));
                javaMailSender.send(simpleMailMessage);
                log.info("激活邮件已发送至" + email);
            }
        }).start();
    }

    @Override
    public void verify(UserVerifyRequest request) {

    }
}
